﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Diagnostics;

namespace ChangDiTools {
    public class Tran {
        [CommandMethod("GetTransTime")]
        public void GetTransTime() {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Stopwatch watch = new Stopwatch();
            PromptSelectionResult res = ed.SelectAll();
            if (res.Status != PromptStatus.OK) {
                return;
            }
            ed.WriteMessage("\nCount of entities : {0}", res.Value.Count.ToString());

            watch.Start();
            using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) {
                foreach (ObjectId id in res.Value.GetObjectIds()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
                    ent.ColorIndex = 1;
                }
                trans.Commit();
            }
            watch.Stop();
            ed.WriteMessage("\nOne Transaction write cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());

            watch.Reset();
            watch.Start();
            using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) {
                foreach (ObjectId id in res.Value.GetObjectIds()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForRead) as Entity;
                    int nColor = ent.ColorIndex;
                }
            }
            watch.Stop();
            ed.WriteMessage("\nOne Transaction read cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());

            watch.Reset();
            watch.Start();
            using (OpenCloseTransaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartOpenCloseTransaction()) {
                foreach (ObjectId id in res.Value.GetObjectIds()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
                    ent.ColorIndex = 2;
                }
                trans.Commit();
            }
            watch.Stop();
            ed.WriteMessage("\nOne OpenCloseTransaction write cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());

            watch.Reset();
            watch.Start();
            using (OpenCloseTransaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartOpenCloseTransaction()) {
                foreach (ObjectId id in res.Value.GetObjectIds()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForRead) as Entity;
                    int nColor = ent.ColorIndex;
                }
            }
            watch.Stop();
            ed.WriteMessage("\nOne OpenCloseTransaction read cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());

            watch.Reset();
            watch.Start();
            foreach (ObjectId id in res.Value.GetObjectIds()) {
                using (OpenCloseTransaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartOpenCloseTransaction()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForRead) as Entity;
                    int nColor = ent.ColorIndex;
                }
            }
            watch.Stop();
            ed.WriteMessage("\nMulty OpenCloseTransaction read cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());


            watch.Reset();
            watch.Start();
            foreach (ObjectId id in res.Value.GetObjectIds()) {
                using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()) {
                    Entity ent = trans.GetObject(id, OpenMode.ForRead) as Entity;
                    int nColor = ent.ColorIndex;
                }
            }
            watch.Stop();
            ed.WriteMessage("\nMulty Transaction read cost: {0}(ms)", watch.ElapsedMilliseconds.ToString());
        }
    }
}
